/***************************************************************
 * Name:      IrReaderMain.h
 * Purpose:   Defines Application Frame
 * Author:    Jimmy F.Klarke (isjfk@163.com)
 * Created:   2009-08-15
 * Copyright: Jimmy F.Klarke (http://www.isjfk.org)
 * License:
 **************************************************************/

#ifndef IRREADERMAIN_H
#define IRREADERMAIN_H

//(*Headers(IrReaderFrame)
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/checkbox.h>
#include <wx/panel.h>
#include <wx/choice.h>
#include <wx/button.h>
#include <wx/frame.h>
#include <wx/statusbr.h>
//*)

#include "IrReaderClient.h"

#define CONFIG_SERIAL_PORT                  _T("/Serial/Port")
#define CONFIG_SERIAL_BAUD                  _T("/Serial/Baud")
#define CONFIG_SERIAL_DATABITSIZE           _T("/Serial/DataBitSize")
#define CONFIG_SERIAL_STOPBITSIZE           _T("/Serial/StopBitSize")
#define CONFIG_SERIAL_PARITY                _T("/Serial/Parity")
#define CONFIG_SERIAL_FLOWCTRL              _T("/Serial/FlowControl")

#define CONFIG_MCU_WRITESIGLEN              _T("/MCU/WriteSignalLength")
#define CONFIG_MCU_WRITESIGLEN_DEFAULT      true

#define CONFIG_OUTPUT_HEX                   _T("/Output/Hex")
#define CONFIG_OUTPUT_HEX_DEFAULT           false
#define CONFIG_OUTPUT_BINARY                _T("/Output/Binary")
#define CONFIG_OUTPUT_BINARY_DEFAULT        true

class ClientThread;

class IrReaderFrame: public wxFrame
{
    public:

        IrReaderFrame(wxWindow* parent,wxWindowID id = -1);
        virtual ~IrReaderFrame();

        static const long ID_CLIENT_DATA;
        static const long ID_CLIENT_STOP;

    private:

        //(*Handlers(IrReaderFrame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnStartButtonClick(wxCommandEvent& event);
        void OnStopButtonClick(wxCommandEvent& event);
        void OnExitButtonClick(wxCommandEvent& event);
        void OnClose(wxCloseEvent& event);
        void OnResetButtonClick(wxCommandEvent& event);
        void OnOutputHexCheckBoxClick(wxCommandEvent& event);
        void OnOutputBinaryCheckBoxClick(wxCommandEvent& event);
        void OnClearOutputButtonClick(wxCommandEvent& event);
        //*)

        void OnIrReaderClientData(wxCommandEvent& event);
        void OnIrReaderClientStop(wxCommandEvent& event);

        void showOutputHex(bool show);
        void showOutputBinary(bool show);

        void initSerialParam(IrReaderClient* irReaderClient);
        wxString getPortDefault();
        wxString getBaudDefault();
        wxString getDataBitSizeDefault();
        wxString getStopBitSizeDefault();
        wxString getParityDefault();
        wxString getFlowCtrlDefault();
        void readConfig();
        void writeConfig();
        void resetConfig();

        void writeSignals(vector<unsigned char> signals);

        //(*Identifiers(IrReaderFrame)
        static const long ID_STATICTEXT1;
        static const long ID_CHOICE_PORT;
        static const long ID_STATICTEXT2;
        static const long ID_CHOICE_DATABITSIZE;
        static const long ID_STATICTEXT3;
        static const long ID_CHOICE_PARITY;
        static const long ID_STATICTEXT4;
        static const long ID_CHOICE_BAUD;
        static const long ID_STATICTEXT5;
        static const long ID_CHOICESTOPBITSIZE;
        static const long ID_STATICTEXT6;
        static const long ID_CHOICE_FLOWCTRL;
        static const long ID_CHECKBOX_WRITESIGLEN;
        static const long ID_CHECKBOX_OUTHEX;
        static const long ID_CHECKBOX_OUTBINARY;
        static const long ID_BUTTON_START;
        static const long ID_BUTTON_STOP;
        static const long ID_BUTTON_CLEAROUTPUT;
        static const long ID_BUTTON_RESET;
        static const long ID_BUTTON_EXIT;
        static const long ID_TEXTCTRL_IRSIGNAL;
        static const long ID_TEXTCTRL_IRSIGNALBINARY;
        static const long ID_PANEL_MAIN;
        static const long ID_STATUSBAR;
        //*)

        //(*Declarations(IrReaderFrame)
        wxStaticBoxSizer* irSignalHexStaticBoxSizer;
        wxChoice* parityChoice;
        wxCheckBox* outputHexCheckBox;
        wxButton* resetButton;
        wxStaticText* StaticText2;
        wxChoice* dataBitSizeChoice;
        wxStaticText* StaticText6;
        wxChoice* baudChoice;
        wxCheckBox* writeSigLenCheckBox;
        wxStaticText* StaticText1;
        wxStaticText* StaticText3;
        wxTextCtrl* irSignalHexTextCtrl;
        wxCheckBox* outputBinaryCheckBox;
        wxStaticBoxSizer* irSignalBinaryStaticBoxSizer;
        wxButton* clearOutputButton;
        wxButton* startButton;
        wxStaticText* StaticText5;
        wxPanel* PanelMain;
        wxStatusBar* statusBar;
        wxButton* stopButton;
        wxTextCtrl* irSignalBinaryTextCtrl;
        wxChoice* flowCtrlChoice;
        wxChoice* stopBitSizeChoice;
        wxButton* exitButton;
        wxStaticText* StaticText4;
        wxChoice* portChoice;
        wxBoxSizer* outputBoxSizer;
        //*)

        ClientThread* clientThread;
        IrReaderClient* irReaderClient;
        unsigned char signalSize;
        unsigned char signalReceived;

        DECLARE_EVENT_TABLE()
};

class ClientThread : public wxThread
{
    public:
        ClientThread(wxFrame* frame, IrReaderClient* client);
        virtual void *Entry();
        virtual void OnExit();

        void pushSignals(vector<unsigned char> signals);
        vector<unsigned char> popSignals();

    private:
        wxFrame* frame;
        IrReaderClient* client;
        vector<unsigned char> signals;
        wxMutex signalLock;
};

#endif // IRREADERMAIN_H
